package com.hwj.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.hwj.service.UserSearchHistoryService;
import com.hwj.utils.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping("/search")
@Slf4j
public class UserSearchHistoryCotroller {
    @Autowired
    UserSearchHistoryService userSearchHistoryService;

    /**
     * 查询搜索记录
     * @param request
     * @return
     */
    @GetMapping("")
    public JsonResponse listByUserId(HttpServletRequest request){
        JWT jwt = JWTUtil.parseToken(request.getHeader("token"));
        Long userId = Long.parseLong(jwt.getPayload("userId")+"");
        return JsonResponse.successResponse(0,"查询历史记录成功",userSearchHistoryService.listByUserId(userId));
    }

    /**
     * 根据搜索id删除搜索记录
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public JsonResponse deleteById(@PathVariable Long id){
        return JsonResponse.successResponse(0,"删除该条消息记录成功",userSearchHistoryService.deleteById(id));
    }

    @GetMapping("/hot")
    public JsonResponse listHot(){
        return JsonResponse.successResponse(0,"查询近一个月热门搜索记录成功",userSearchHistoryService.listHot());
    }
}
